/*
  BoardIO.cpp, Version 1.0 beta
  Oct 2010, by Uwe Gartmann
*/

/// Includes
#include <BoardIO.h>
#include <Common.h>

extern "C" {
	#include <avr/io.h>
	#include <avr/interrupt.h>
	#include "wiring.h"
	#include "pins_arduino.h"
}

// Declarations
uint8_t SlideSwitch_Mode = 0;

BoardIO::BoardIO()
{
// Class constructor
}

// Public Methods --------------------------------------------------------------------
void BoardIO::Init(void) {
	pinMode(C_LED_PIN, OUTPUT);			// GPS status LED
	pinMode(A_LED_PIN, OUTPUT);			// GPS status LED
	pinMode(B_LED_PIN, OUTPUT);			// GPS status LED
	pinMode(SLIDE_SWITCH_PIN, INPUT);	// To enter interactive mode
	pinMode(PUSHBUTTON_PIN, INPUT);		// 
	pinMode(RELAY_PIN, OUTPUT);			// 
	DDRF |= B11000000;					// Set Debug Pins PF6, PF7 for OUTPUT
	SlideSwitch_Mode = digitalRead(SLIDE_SWITCH_PIN);
}

void BoardIO::DebugPin(uint8_t p, uint8_t m) {
	switch (m) {
		case ON:
			PORTF |= p;
			break;
		case OFF:
			PORTF &= ~p;
			break;
		case TOGGLE:
			uint8_t f = PORTF;
			if (f & p) {
				PORTF &= ~p;
			} else {
				PORTF |= p;
			}
	}
}

void BoardIO::Relay(uint8_t m) {
	switch (m) {
		case ON:
			digitalWrite(RELAY_PIN, ON);
			break;
		case OFF:
			digitalWrite(RELAY_PIN, OFF);
			break;
		case TOGGLE:
			if (digitalRead(RELAY_PIN) == ON) {
				digitalWrite(RELAY_PIN, OFF);
			} else {
				digitalWrite(RELAY_PIN, ON);
			}
	}
}

void BoardIO::LED(uint8_t l, uint8_t m) {
	switch (m) {
		case ON:
			digitalWrite(l, ON);
			break;
		case OFF:
			digitalWrite(l, OFF);
			break;
		case TOGGLE:
			if (digitalRead(l) == ON) {
				digitalWrite(l, OFF);
			} else {
				digitalWrite(l, ON);
			}
	}
}

uint8_t BoardIO::SlideSwitchMode(void) {
	return SlideSwitch_Mode;
}